const path = require('path')
const allEntries = require("./entry")
const pages = {}

const pagesTitleMap = {
    delivery_address: '收获地址',
    vip_info: '会员信息',
    my_order: '我的订单',
    nearby_goods: '附近商品',
    about_us: '关于我们',
    cart: '购物车',
    check_order: '订单支付',
    catetory: '分类',
    person_center: '个人中心',
    product: '商品',
    product_detail: '商品详情'
}

function resolve (dir) {
    return path.join(__dirname, dir)
}

let routerPages = ['vip_info','my_order', 'check_order', 'my_coupon']

for(var attr in allEntries) {
    pages[attr] = {
        entry: allEntries[attr],
        template: 'public/index.html',
        filename: attr+".html",
        title: pagesTitleMap[attr]||"",
        pageName: attr,
        chunks: routerPages.includes(attr) ? ['chunk-vendors', 'chunk-common', 'vueRouter', attr] : ['chunk-vendors', 'chunk-common', attr]
    }
}

module.exports = {
    chainWebpack: (config)=> {
        config.resolve.alias
            .set('@', resolve('src'))
            .set('@utils',resolve('utils'))
            .set('@css',resolve('css'))
            .set('@components', resolve('components'))
            .set('@common', resolve('common'))
    },
    configureWebpack:(config) => {
        config.externals = {
            'vue': 'Vue',
            'wx': 'wx',
            'axios': 'axios'
        }
        if(process.env.NODE_ENV == 'production') {
            config.optimization = {
                splitChunks: {
                    chunks: 'all',
                    cacheGroups: {
                        vueRouter: {
                            name: 'vueRouter',
                            test: /[\\/]node_modules[\\/]vue-router/,
                            priority: 20,
                            chunks: 'all'
                        },

                        // 抽离node_modules下的库为一个chunk
                        vendors: {
                            name: 'chunk-vendors',
                            test: /[\\/]node_modules[\\/]/,
                            minChunks: 6,
                            chunks: 'initial',
                            priority: 2,
                            reuseExistingChunk: true,
                            enforce: true
                        },
                         // 抽离所有入口的公用资源为一个chunk
                        common: {
                            name: 'chunk-common',
                            chunks: 'initial',
                            minChunks: 6,
                            maxInitialRequests: 5,
                            minSize: 0,
                            priority: 1,
                            reuseExistingChunk: true,
                            enforce: true
                        },
                    }
                }
            }
        }
    },
    filenameHashing: true,
    lintOnSave: false,
    baseUrl: "./",
    outputDir: process.env.VUE_APP_OUTPUT_DIR,
    assetsDir: "asset",
    productionSourceMap: !(process.env.NODE_ENV=="production"),
    pages: pages
}